import { TOPIC_LISTS, ADD_LIST, TOGGLR_SCROLL_SHOW } from "./types";
import { fromJS } from "immutable";
import axios from "axios";

const changeHomeData = result => ({
  type: TOPIC_LISTS,
  topicList: result.topicList,
  articleList: result.articleList,
  recommendList: result.recommendList
});

const addHomeList = (list, nextPage) => ({
  type: ADD_LIST,
  list: fromJS(list),
  nextPage
});

export const getHomeInfo = () => {
  return dispatch => {
    axios.get("/api/home.json").then(res => {
      const result = res.data.data;
      dispatch(changeHomeData(result));
    });
  };
};

export const getMoreList = page => {
  return dispatch => {
    axios.get("/api/homeList.json?page=" + page).then(res => {
      const result = res.data.data;
      dispatch(addHomeList(result, page + 1));
    });
  };
};

export const toggleTopShow = show => ({
  type: TOGGLR_SCROLL_SHOW,
  show
});
